<?php

namespace app\controllers;

use Yii;
use app\models\WorkAccount;
use app\models\WorkAccountSearch;
use app\models\AccProject;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * WorkAccountController implements the CRUD actions for WorkAccount model.
 */
class WorkAccountController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all WorkAccount models.
     * @return mixed
     */
    public function actionIndex($acc_pro_id=null)
    {
        $searchModel = new WorkAccountSearch();
        $queryParams = array_merge(array(),Yii::$app->request->getQueryParams());
		
		if($acc_pro_id!=null)
		{
			$queryParams['WorkAccountSearch']['acc_pro_id'] = $acc_pro_id;
		}
        $dataProvider = $searchModel->search($queryParams);

        return $this->render($acc_pro_id==null?'index':(!Yii::$app->user->isGuest && Yii::$app->user->identity->acc_type==0?'user':'user_view'), [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single WorkAccount model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new WorkAccount model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($acc_pro_id=null)
    {
        $model = new WorkAccount();
        $modelAccProject = new AccProject();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
			$model->acc_pro_id = $acc_pro_id;
			//nếu không có acc_pro_id thì phải chọn acc_pro_id
			if($acc_pro_id==null)
			{
				return $this->render('create', [
					'model' => $model,
					'arrayAccProject' => AccProjectController::getAccProject(),
				]);
			}
			else
			{
				return $this->render('create', [
					'model' => $model,
					'arrayAccProject' => AccProjectController::getAccProject(),
				]);
			}
        }
    }

    /**
     * Updates an existing WorkAccount model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing WorkAccount model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the WorkAccount model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return WorkAccount the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = WorkAccount::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
